/*
 * SmartPointer.h
 *
 *  Created on: Sep 29, 2010
 *      Author: nicoabie
 */

#ifndef SMARTPOINTER_H_
#define SMARTPOINTER_H_

#include "WeakPointer.hpp"
#include "Cache.hpp"

struct RefCounter
{
	RefCounter (): totalRefs_(0), strongRefs_(0), dirty_(false){}
	bool dirty_;
	long totalRefs_;
	long strongRefs_;
};

template<class T, class Cache>
class SmartPointer
{
public:
	SmartPointer();
	SmartPointer(const T* x);
	~SmartPointer();
	template<class U>
	SmartPointer(const SmartPointer<U,Cache>& x);
	template<class U>
	SmartPointer& operator=(SmartPointer<U,Cache>& x);
	template<class U>
	SmartPointer(const WeakPointer<U,Cache>& x);
	template<class U>
	SmartPointer& operator=(WeakPointer<U,Cache>& x);
	T& operator*() const;
	T* operator->() const;
	RefCounter& getCounter();
	T& get();
	void setID(long id);
	long getID();
	void read(long id);
	void write(long id);
	bool dirty();

private:
	RefCounter counter;
	T* pRefered;
	long id;
	void release();

};

#endif /* SMARTPOINTER_H_ */
